package StudyCode.summary.csv;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class EscapeCSVColumn {
	private static final String CSV_DELIMITER = ",";
	private static final String CSV_QUOTE = "\"";
	private static final String LF = "\n";
	private static final String CR = "\r";

	public static void main(String[] args) {
		String str = "www,.tah1,986.c,om";
		System.out.println("转义前:" + str);
		System.out.println("转义后:" + escapeColumn("www.tah1986.com"));
	}

	public static String escapeColumn(String input) {

		if (input == null || input.length() == 0) {
			return "";
		}
		Boolean contCSVDel = input.contains(CSV_DELIMITER);
		Boolean contCSVQuote = input.contains(CSV_QUOTE);
		Boolean contLF = input.contains(LF);
		Boolean contCR = input.contains(CR);
		if (!(contCSVDel || contCSVQuote || contLF || contCR)) {
			return input;
		} else {
			String retVal = "";
			retVal += CSV_QUOTE;
			if (contCSVQuote)
				retVal += input.replace(CSV_QUOTE, CSV_QUOTE + CSV_QUOTE);
			else
				retVal += input;
			retVal += CSV_QUOTE;
			return retVal;
		}

	}
}
